﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using LiveSequence.Suite.Interfaces;
using LiveSequence.Suite.Documents;

namespace LiveSequence.Suite.Views
{
  public partial class ListObjectInstanceView : BaseView<SeqDiagramDoc>
  {
    private DocManager _docManager;

    public ListObjectInstanceView()
    {
      InitializeComponent();
      InitializeComponentEx();

      _docManager = DocManager.Instance;
      _docManager.ActiveDocumentChanged += new EventHandler(_docManager_ActiveDocumentChanged);
      SelectedDocumentChanged += new EventHandler(ListObjectInstanceView_SelectedDocumentChanged);

    }

    void ListObjectInstanceView_SelectedDocumentChanged(object sender, EventArgs e)
    {
      _dgvObjectInstances.DataSource = SelectedDocument.Diagram.SequenceData.GetObjectList();
    }

    void _docManager_ActiveDocumentChanged(object sender, EventArgs e)
    {
      SeqDiagramDoc document = _docManager.ActiveDocument as SeqDiagramDoc;
      if (document != null)
      {
        SelectedDocument = document;
      }

    }

    private void InitializeComponentEx()
    {
      _dgvObjectInstances.AutoGenerateColumns = false;

      DataGridViewColumn column = new DataGridViewTextBoxColumn();
      column.HeaderText = "Object";
      column.DataPropertyName = "TypeName";
      column.ReadOnly = true;
      column.AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells;
      _dgvObjectInstances.Columns.Add(column);

      column = new DataGridViewCheckBoxColumn();
      column.HeaderText = "Visible";
      column.DataPropertyName = "Visible";
      column.AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells;
      _dgvObjectInstances.Columns.Add(column);
      column = new DataGridViewTextBoxColumn();
      column.HeaderText = "Namespace";
      column.DataPropertyName = "NameSpace";
      column.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
      _dgvObjectInstances.Columns.Add(column);


    }

    private void _dgvObjectInstances_SelectionChanged(object sender, EventArgs e)
    {
      if(_dgvObjectInstances.SelectedCells.Count == 1)
      {
        int iRowSelected = _dgvObjectInstances.SelectedCells[0].RowIndex;
        DataGridViewRow row = _dgvObjectInstances.Rows[iRowSelected];
        object o = row.DataBoundItem;
        PropertyViewManager.Instance.SelectedObject = o;
      }
    }

    private void ListObjectInstanceView_Leave(object sender, EventArgs e)
    {
      _dgvObjectInstances.EndEdit();
    }
  }
}
